package com.shsubway.activity.manger;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.RelativeLayout;
import android.widget.Spinner;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.shsubway.R;
import com.shsubway.activity.staff.LookQuestionsActivity;
import com.shsubway.adapter.AllQAdapter;
import com.shsubway.base.BaseActivity;
import com.shsubway.model.AnswerEntity;
import com.shsubway.model.QuestionEntity;
import com.shsubway.utils.Constant;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

/**
 * 按照类型查看题库
 */
public class AllQuestionActivity extends BaseActivity implements BaseQuickAdapter.RequestLoadMoreListener {

    @BindView(R.id.choose_spinner)
    Spinner chooseSpinner;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.my_rec)
    RecyclerView myRec;
    @BindView(R.id.my_swf)
    SwipeRefreshLayout mySwf;
    @BindView(R.id.content_all_question)
    RelativeLayout contentAllQuestion;
    AllQAdapter allQAdapter;
    List<QuestionEntity> questionEntityList;
    private int limit = 10;        // 每页的数据是10条
    private int curPage = 0;        // 当前页的编号，从0开始
    private int QUES_TYPE = 0;
    Intent intent;
    Bundle bundle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all_question);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(v -> finish());


        chooseSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (position != QUES_TYPE) {
                    QUES_TYPE = position;
                    curPage = 0;
                    addData();
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        mySwf.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                curPage = 0;
                addData();
            }
        });
        myRec.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                // TODO: 2017/3/2 判断登录人的身份 如果是 管理则进入 ************
                //   如果是员工则进入题库的查看
                bundle.putSerializable(Constant.QUESTION_LIST, (Serializable) adapter.getData());
                bundle.putSerializable(Constant.QUESTION, (Serializable) adapter.getItem(position));
                bundle.putInt(Constant.QUESTION_NO, position);

                intent.putExtras(bundle);
                intent.setClass(context, LookQuestionsActivity.class);
                startActivity(intent);
            }
        });
    }

    private void initView() {
        intent = getIntent();
        bundle = intent.getExtras();
        setTitleBar(toolbar, "查看");
        chooseSpinner.setSelection(QUES_TYPE);
        initAdapter();
        addData();
    }

    private void addData() {
        BmobQuery<QuestionEntity> query = new BmobQuery<>();
        query.addWhereEqualTo("qsttype", QUES_TYPE);
        // 如果是加载更多
        query.setLimit(limit);
        if (curPage != 0) {
            query.setSkip(curPage * limit);
        } else {
            query.setSkip(curPage);
        }
        query.findObjects(new FindListener<QuestionEntity>() {
            @Override
            public void done(List<QuestionEntity> list, BmobException e) {
                mySwf.setRefreshing(false);
                if (e == null) {
                    Log.e("查询成功：共", list.size() + "条数据。");
                    if (curPage == 0) {
                        allQAdapter.setNewData(list);
                    } else {
                        allQAdapter.addData(list);
                    }
                    if (list.size() != 10) {
                        allQAdapter.loadMoreEnd(false);
                    } else {
                        allQAdapter.loadMoreComplete();
                        curPage++;
                    }
                } else {
                    Log.e("bmob", "失败：" + e.getMessage() + "," + e.getErrorCode());
                }
            }
        });


    }

    private void initAdapter() {
        questionEntityList = new ArrayList<>();
        setRecy(myRec);
        setRefresh(mySwf);
        allQAdapter = new AllQAdapter(R.layout.item_all_qes, questionEntityList);
        allQAdapter.setEmptyView(emptyView);
        allQAdapter.setOnLoadMoreListener(this);
        allQAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_LEFT);
        myRec.setAdapter(allQAdapter);
    }

    @Override
    public void onLoadMoreRequested() {
        addData();
    }
}
